﻿Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Drawing
Partial Class Public_LoginVerificationCode
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack OrElse Session("LoginCheckCode") Is Nothing Then
            SetCode()
        End If

    End Sub
    Public Sub SetCode()
        Dim checkCode As String = Me.CreateRandomCode(4)
        Session("LoginCheckCode") = checkCode
        CreateImage(checkCode)
    End Sub

End Class
Partial Class Public_LoginVerificationCode
    Public Function CreateRandomCode(ByVal codeCount As Integer) As String
        Dim allChar As String = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"
        Dim allCharArray() As String = allChar.Split(",")
        Dim randomCode As String = ""
        Dim temp As Integer = -1

        Dim rand As New Random()
        For i As Integer = 0 To codeCount - 1
            If temp <> -1 Then
                rand = New Random(i * temp * CInt(DateTime.Now.Second))
            End If
            Dim t As Integer = rand.[Next](allCharArray.Length)
            If temp <> -1 AndAlso temp = t Then
                Return CreateRandomCode(codeCount)
            End If
            temp = t
            randomCode += allCharArray(t)
        Next
        Return randomCode
    End Function
    Private Sub CreateImage(ByVal checkCode As String)
        Dim image As New System.Drawing.Bitmap(Convert.ToInt32(Math.Ceiling(CDec(checkCode.Length * 14))), 22)
        Dim g As Graphics = Graphics.FromImage(image)

        Try
            Dim random As New Random()
            g.Clear(Color.AliceBlue)

            For i As Integer = 0 To 24
                Dim x1 As Integer = random.[Next](image.Width)
                Dim x2 As Integer = random.[Next](image.Width)
                Dim y1 As Integer = random.[Next](image.Height)
                Dim y2 As Integer = random.[Next](image.Height)

                g.DrawLine(New Pen(Color.Silver), x1, y1, x2, y2)
            Next

            Dim font As Font = New System.Drawing.Font("Comic Sans MS", 12, System.Drawing.FontStyle.Bold)
            Dim brush As New System.Drawing.Drawing2D.LinearGradientBrush(New Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2F, True)
            g.DrawString(checkCode, font, New SolidBrush(Color.Black), 2, 2)

            For i As Integer = 0 To 99
                Dim x As Integer = random.[Next](image.Width)
                Dim y As Integer = random.[Next](image.Height)

                image.SetPixel(x, y, Color.FromArgb(random.[Next]()))
            Next
            g.DrawRectangle(New Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1)

            Dim ms As New System.IO.MemoryStream()

            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif)

            Response.ClearContent()
            Response.ContentType = "image/Gif"
            Response.BinaryWrite(ms.ToArray())
        Finally
            g.Dispose()
            image.Dispose()
        End Try
    End Sub

End Class